package com.epower.tools;

import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    public static byte[] readTarFile(File tarFile, String suffix) {
        try {
            ArchiveInputStream archiveInputStream = new ArchiveStreamFactory()
                    .createArchiveInputStream("tar", new BufferedInputStream(new FileInputStream(tarFile)));
            TarArchiveEntry entry = null;
            while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) {
                if (entry.getSize() > 0) {
                    if (entry.getName().endsWith(suffix)) {
                        return getContent(archiveInputStream, entry.getSize());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<byte[]> readTarFile(File tarFile) {
        List res = new ArrayList();
        try {
            ArchiveInputStream archiveInputStream = new ArchiveStreamFactory()
                    .createArchiveInputStream("tar", new BufferedInputStream(new FileInputStream(tarFile)));
            TarArchiveEntry entry = null;
            while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) {
                if (entry.getSize() > 0) {
                    if (entry.getName().endsWith("cfg")) {
                        res.add(getContent(archiveInputStream, entry.getSize()));
                    }else if (entry.getName().endsWith("dat")) {
                        res.add(getContent(archiveInputStream, entry.getSize()));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] getContent(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            byte[] buffer = new byte[1024];
            while (true) {
                int len = is.read(buffer);

                if (len == -1) {
                    break;
                }
                baos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos.toByteArray();
    }

    public static byte[] getContent(InputStream fi, long fileSize) throws IOException {
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }
        // 确保所有数据均被读取
        if (offset != buffer.length) {
            throw new IOException("Could not completely read file ");
        }
        fi.close();
        return buffer;
    }

    public byte[] getContent(String filePath) throws IOException {
        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }
        // 确保所有数据均被读取
        if (offset != buffer.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        fi.close();
        return buffer;
    }
}
